<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
// namespace Admin\Controller;
namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class CartController extends HomeController {
	public function index(){
		if (!isset($_SESSION['onethink_home']['user_auth']['uid'])) {
			 $this->error('请先登录',U('Login/login')); 

		}else{
		$map['status'] = 0;
		$map['uid'] = $_SESSION['onethink_home']['user_auth']['uid'];
		$order = M('order')->where($map)->select();
		$count = M('order')->where($map)->count();
		$sumprice = M('order')->where($map)->sum('sumprice');
		foreach ($order as $k => $v) {
			$goods=M('goods')->where(array('id'=>$v['gid']))->field('id,icon,name')->find();
			$path=M('picture')->where(array('id'=>$goods['icon']))->field('path')->find();
			$attrval=M('attrval')->where(array('id'=>array('in',$v['attrvid'])))->field('value')->select();
			$order[$k]['gname'] = $goods['name'];
			$order[$k]['attrval']=implode(',',array_column($attrval,'value'));
			$order[$k]['pic'] = $path['path'];
		}
		// var_dump($order);
		$this->assign('order',$order);
		$this->assign('count',$count);
        $this->assign('sum',$sum);
        $this->assign('sumprice',$sumprice);

		$this->display();
		}
		
	}



	public function add(){

		if (isset($_SESSION['onethink_home']['user_auth']['uid'])) {
			$_POST['uid'] = $_SESSION['onethink_home']['user_auth']['uid'];
			$_POST['status']=0;
			$_POST['addtime']=time();
			$_POST['express']=time();
			$_POST['attrvid']=implode(',',$_POST['attrvid']);
			$res=M('order')->add($_POST);

			if ($res) {
				$data['status']=1;
				$data['info']="添加成功";
			}else{
				$data['status'] = 0;
    		    $data['info'] = '请先登录';
			}
		}else{
			$this->error('先登录',U('User/login'));
		}

		$this->ajaxReturn($data);
	}
	public function update(){
		// var_dump(I('post.'));
		$res=M('order')->save(I('post.'));
		if ($res) {
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}
		$this->ajaxReturn($data);
	}
	
}
